import unittest
from _algo import *

class TestPermuations(unittest.TestCase):

    def testNoCombinations(self):
        self.assertEquals( [[]],  sorted(xcombinations([],  0)) )
        self.assertEquals( [[]],  sorted(xcombinations([1],  0)) )
        self.assertEquals( [[]],  sorted(xcombinations([1, 2],  0)) )

    def testNoPermutations(self):
        self.assertEquals( [[]],  sorted(xpermutations([])) )
        self.assertEquals( [[1]],  sorted(xpermutations([1])) )

    def testPermutations2(self):
        ps = sorted(xpermutations([1, 2]))
        self.assertEquals(ps,  [ [1, 2],  [2, 1] ])

    def testPermutations3(self):
        ps = sorted(xpermutations([1, 2,  3]))
        self.assertEquals(ps,  [ [1, 2, 3], [1, 3, 2],  [2, 1, 3],  [2, 3, 1],  [3, 1, 2],  [3, 2, 1] ])

    def testPermutationsInputOrder(self):
        ps1 = sorted(xpermutations([1, 2,  3,  4,  5]))
        ps2 = sorted(xpermutations([5,  4,  3,  2,  1]))
        self.assertEquals(ps1,  ps2)

    def testPermutationsDubs(self):
        ps1 = sorted(xpermutations([1, 1, 2]))
        self.assertEquals(ps1, [ [1, 1, 2],  [1, 1, 2],  [1, 2, 1], [1, 2, 1], [2, 1, 1], [2, 1, 1] ])

class TestSplitby(unittest.TestCase):
    def testEmpty(self):
        yes = lambda x: True
        self.assertEquals( ([], []), splitby(yes, []))
    def testYes(self):
        yes = lambda x: True
        self.assertEquals( ([2, 3, 1], []), splitby(yes, [2, 3, 1]))
    def testNo(self):
        no = lambda x: False
        self.assertEquals( ([],  [2, 3, 1]), splitby(no, [2, 3, 1]))
    def testEven(self):
        even = lambda n: n%2==0
        self.assertEquals( ([2],  [3, 1]), splitby(even, [2, 3, 1]))

if __name__ == '__main__':
    unittest.main()
